
<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="searchParam.name" />
      </el-form-item>
      <el-form-item label="中文名称">
        <el-input v-model="searchParam.cnname" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="path"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="cnname" label="中文名称" />
      <el-table-column prop="name" label="名称" />
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <el-button size="small" @click="editRow(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tablePageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableTotal"
        prev-text="上一页"
        next-text="下一页"
      />
    </div>
    <el-dialog :visible.sync="dialogFormVisible" title="新增">
      <el-form :model="form">
        <el-form-item label="中文名称" :label-width="formLabelWidth">
          <el-input v-model="form.cnname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from '@/api/resource/cate'

export default {
  data() {
    return {
      tableData: [],
      tablePageSize: 20,
      tablePage: 2,
      tableTotal: 21,
      listLoading: true,
      searchParam: {
        path: '',
        name: ''
      },
      dialogFormVisible: false,
      form: {
        path: '',
        name: '',
        title: ''
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.tableData = response.data
        this.listLoading = false
      })
    },
    editRow(row) {
      console.log('edit row: ' + row.id)
    },
    deleteRow(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 执行调用删除接口
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addRow() {
      console.log('add row')
    }
  }
}
</script>

<style>

</style>
